package com.controller;

import com.entity.Result;
import com.service.BaseService;
import com.utils.ResultUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseController<E, P> {




    /**
     * @Author admin
     * @Description 将实体类中的值放入map
     * @Date 2019/1/25 16:45
     **/
    public Map<String, Object> putEntityInMap(E entity) {
        Class<?> aClass = entity.getClass();//获取类对象
        Field[] fields = aClass.getDeclaredFields();//获取对象的属性名数组
        Map<String, Object> query = new HashMap<>();//查询map
        try {
            for (int i = 0; i < fields.length; i++) {
                String name = fields[i].getName();
                String firstLetter = name.substring(0, 1).toUpperCase();
                String getter = "get" + firstLetter + name.substring(1);//构造get方法的方法名
                Method method = aClass.getMethod(getter, new Class[]{});
                Object value = method.invoke(entity, new Object[]{});//获取属性的值
                if (value != null) {
                    query.put(name, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return query;
    }




}
